﻿using System;
using System.Text.RegularExpressions;

namespace ReverseSentance
{
    class ReverseSentance
    {
        static void Main(string[] args)
        {
            string text = "A lass is picking, berries.";
            text = ReverseSentence(text);

            string replacePattern = @"\w+\Z";
            string newText = Regex.Replace(text, replacePattern, new MatchEvaluator(LowerCase));

            replacePattern = @"\A\.\w*";
            newText = Regex.Replace(newText, replacePattern, new MatchEvaluator(UpperCase));
            Console.WriteLine(newText);
        }

        static string LowerCase(Match aMatch)
        {
            string result = aMatch.Value;
            result = Char.ToLower(result[0]) + result.Substring(1) + ".";
            return result;
        }

        static string UpperCase(Match aMatch)
        {
            string result = aMatch.Value;
            result = Char.ToUpper(result[1]) + result.Substring(2);
            return result;
        }

        private static string ReverseSentence(string text)
        {
            Regex separator = new Regex(@"\W|(\w*)");
            MatchCollection collection = separator.Matches(text);
            text = "";
            foreach (var item in collection)
            {
                text = item + text;
            }
            return text;
        }
    }
}